package com.asistencia.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asistencia.businness.CareerBusiness;
import com.asistencia.businness.CourseBusinness;
import com.asistencia.businness.EnrollmentBusiness;
import com.asistencia.businness.SemesterBusiness;
import com.asistencia.businness.StudentBusiness;
import com.asistencia.domain.Career;
import com.asistencia.domain.Course;
import com.asistencia.domain.Enrollment;
import com.asistencia.domain.Semester;
import com.asistencia.domain.Student;

@Controller
public class EnrollmentController {

	@Autowired
	private EnrollmentBusiness enrollmentBusiness;
	@Autowired
	private StudentBusiness studentBusiness;
	@Autowired
	private CareerBusiness careerBusiness;
	@Autowired
	private CourseBusinness courseBusinness;
	@Autowired
	private SemesterBusiness semesterBusiness;

	@RequestMapping(value = "studen-list.tecsup", method = RequestMethod.GET)
	public String getStudenEnrollmentByCourseSemester(
			@RequestParam(value = "ic", required = true) Integer idCourse,
			@RequestParam(value = "is", required = true) Integer idSemester,
			ModelMap modelMap) {

		List<Enrollment> lstEnrollments = enrollmentBusiness
				.listByCourseSemester(idCourse, idSemester);

		Course course = courseBusinness.getCourseById(idCourse);
		Semester semester = semesterBusiness.getById(idSemester);
		
		if (lstEnrollments != null) {
			for (Enrollment enrollment : lstEnrollments) {
				Student student = studentBusiness.getById(enrollment.getMyStudent().getIdStudent());
				Career career = careerBusiness.getById(student.getMyCareer().getIdCareer());
				student.setMyCareer(career);
				enrollment.setMyStudent(student);
			}
		}
		modelMap.addAttribute("course", course);
		modelMap.addAttribute("semester", semester);
		modelMap.addAttribute("lstEnrollments", lstEnrollments);
		
		return "studen-list";

	}

}
